/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.szitar.forum.client;

import hu.szitar.forum.common.Task;
import hu.szitar.forum.common.model.User;
import hu.szitar.forum.server.dao.UserDAO;
import hu.szitar.forum.server.query.HqlType;
import hu.szitar.forum.server.query.QueryParameter;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author tircsil
 */
public class Login implements Task<User>, Serializable {

    private static final long serialVersionUID = 227L;
    private User user;
    /**
     * Construct a task to calculate pi to the specified
     * precision.
     */
    public Login(User user) {
        this.user = user;
    }

    /**
     * Calculate pi.
     */
    public User execute() {
        return login(user);
    }

    /**
     * Compute the value of pi to the specified number of 
     * digits after the decimal point.  The value is 
     * computed using Machin's formula:
     *
     *          pi/4 = 4*arctan(1/5) - arctan(1/239)
     *
     * and a power series expansion of arctan(x) to 
     * sufficient precision.
     */
    public static User login(User user) {
        List<User> users = UserDAO.getInstance().find("SELECT u FROM User u WHERE u.userName = ?", new QueryParameter<String>(HqlType.TEXT, user.getUserName()));
        if( users != null && !users.isEmpty() ){
            return users.get(0);
        }
        return null;
    }
    
}
